// pages/login/forgetPassword/index.js
const { getVCodeRequest, next, confirmPsw } = require('../../../api/login')
const { debounce } = require('lodash')
const { validation } = require('./validate')

Page({
  /**
   * 页面的初始数据
   */
  data: {
    steps: [
      {
        text: '1'
      },
      {
        text: '2'
      },
      {
        text: '3'
      }
    ],
    form: {
      phone: '',
      code: ''
    },
    VCodeText: '发送验证码',
    active: 0,
    btnIsDisabled: false,
    isVFocus: true,
    newPwd: '', // 新密码
    confirmPwd: '' // 确认密码
  },

  showVCode: function (e) {
    this.setData({
      ['form.code']: e.detail.value
    })
  },

  tapFn(e) {
    this.setData({
      isVFocus: true
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.sendCode = debounce(this.sendCode, 300)
    this.nextStep = debounce(this.nextStep, 300)
    this.modify = debounce(this.modify, 300)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},

  onChange: function (v) {
    this.setData({
      ['form.phone']: v.detail
    })
  },

  /**
   * 发送验证码
   */
  sendCode: function () {
    if (this.data.btnIsDisabled) return
    if (/^1[3456789]\d{9}$/.test(this.data.form.phone)) {
      let params = {
        phone: this.data.form.phone,
        type: 2
      }
      this.setData({
        VCodeText: '发送中',
        btnIsDisabled: true
      })
      getVCodeRequest(params)
        .then((res) => {
          let { data, code } = res.data
          if (code === 200) {
            wx.showToast({
              title: '验证码发送成功！',
              icon: 'success'
            })
            let num = 60
            let timer = setInterval(() => {
              this.setData({
                VCodeText: num + '秒后可重新发送'
              })
              num -= 1
              if (num < 0) {
                this.setData({
                  VCodeText: '发送验证码',
                  btnIsDisabled: false
                })
                clearInterval(timer)
              }
            }, 1000)
          }
        })
        .catch(() => {
          this.setData({
            VCodeText: '发送验证码',
            btnIsDisabled: false
          })
        })
    } else {
      wx.showModal({
        title: '提示',
        content: '手机号不能为空且格式不能有误',
        confirmColor: '#3CC51F'
      })
      return
    }
  },

  /**
   * 下一步
   */
  nextStep: function () {
    let validationError = validation({
      active: this.data.active,
      phone: this.data.form.phone,
      code: this.data.form.code
    })

    if (validationError == '') {
      next(this.data.form).then((res) => {
        let { code } = res.data
        if (code === 200) {
          this.setData({
            active: 1
          })
        }
      })
    } else {
      wx.showModal({
        title: '提示',
        content: validationError
      })
    }
  },

  /**
   * 修改
   */
  modify: function () {
    let validationError = validation({
      active: this.data.active,
      newPwd: this.data.newPwd,
      confirmPwd: this.data.confirmPwd
    })
    let params = {
      phone: this.data.form.phone,
      password: this.data.newPwd,
      roleId: '5'
    }
    if (validationError == '') {
      confirmPsw(params).then((res) => {
        let { code } = res.data
        if (code === 200) {
          this.setData({
            active: 2
          })
        }
      })
    }
  },

  /**
   * 退出
   */
  exit: function () {
    wx.redirectTo({
      url: '/pages/login/index'
    })
  }
})
